package com.yc;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一结果返回
 * @title
 * @author kukaha
 * @updateTime  22:25
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="Result对象", description="通用返回数据")
public class Result {

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "code")
    private Integer code;

    @ApiModelProperty(value = "数据描述")
    private String message;

    @ApiModelProperty(value = "总数")
    private Integer total;

    @ApiModelProperty(value = "数据信息")
    private Map<String, Object> data = new HashMap<>();

    public Result() {
    }

    public Result(Result r) {
        this.success = r.success;
        this.code = r.code;
        this.message = r.message;
        this.data = r.data;
    }

    public static Result ok() {
        Result r = new Result();
        r.setSuccess(true);
        r.setCode(R_Code.R_OK.getCode());
        r.setMessage(R_Code.R_OK.getMessage());
        return r;
    }

    public static Result error() {
        Result r = new Result();
        r.setSuccess(false);
        r.setCode(R_Code.R_ERROR.getCode());
        r.setMessage(R_Code.R_ERROR.getMessage());
        return r;
    }

    public Result success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public Result total(Integer total) {
        this.setTotal(total);
        return this;
    }

    public Result message(String message) {
        this.setMessage(message);
        return this;
    }

    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }

    public Result code(R_Code r_Code){
        this.code = r_Code.getCode();
        this.message = r_Code.getMessage();
        return this;
    }

    public Result data(Object value) {
        this.data.put("data", value);
        return this;
    }

    public Result data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public Result data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }

    public JSON buildJSON() {
        return (JSON) JSON.toJSON(new Result(this));
    }

    public Result buildThis(){
        return new Result(this);
    }

    public String buildString() {
        return JSON.toJSONString(this);
    }

    /**
     * JSON字符串转成 R 对象
     * @param json
     */
    public static Result format(String json) {
        try {
            return JSON.parseObject(json, Result.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String toString() {
        return "R{" +
                "success=" + success +
                ", code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }



}